import React, { lazy } from "react";
import{ createBrowserRouter} from 'react-router-dom'
 
import MainLayout from "../layout/MainLayout";
import ManageLayout from "../layout/ManageLayout";
import QuestionLayout from "../layout/QuestionLayout";

import Home from "../pages/Home";
import List from "../pages/manage/List";
import Login from "../pages/Login";
import Register from "../pages/Register";
import NotFound from "../pages/NotFound";
import Trash from "../pages/manage/Trash";
import Star from "../pages/manage/Star";

//使用路由懒加载，拆分bundle ，优化首页体积 
const Edit=lazy(()=>import(/* webpackChunkName "editPage" */'../pages/question/Edit/Index'))
const Stat=lazy(()=>import(/* webpackChunkName "statPage" */'../pages/question/Stat/Index'))

const routerConfig=createBrowserRouter([
    {
        path:'/',
        element:<MainLayout/>,
        children:[
            {
                path:'/',
                element:<Home/>
            },
            {
                path:'login',
                element:<Login/>
            },
            {
                path:'register',
                element:<Register/>
            },
            {
                path:'manage',
                element:<ManageLayout/>,
                children:[
                    {
                        path:'list',
                        element:<List/>
                    },
                    {
                        path:'star',
                        element:<Star/>
                    },
                    {
                        path:'trash',
                        element:<Trash/>
                    }
                ]
            },
            {
                path:'*', //404路由配置
                element:<NotFound/>
            },
        ]
    },
    {
        path:'question',
        element:<QuestionLayout/>,
        children:[
            {
                path:'edit/:id',
                element:<Edit/>
            },
            {
                path:'stat/:id',
                element:<Stat/>
            }
        ]
    },
    {
        path:'*', //404路由配置
        element:<NotFound/>
    },
])
export default routerConfig

// 常用的路由，常量
export const HOME_PATHNAME='/'
export const LOGIN_PATHNAME='/login'
export const REGISTER_PATHNAME='/register'
export const MANAGE_TRASH_PATHNAME='/manage/trash'
export const MANAGE_INDEX_PATHNAME='/manage/list'
export const MANAGE_STAR_PATHNAME='/manage/star'

//是否是登录注册页面
export function isLoginOrRegister(pathname:string){
    if([LOGIN_PATHNAME,REGISTER_PATHNAME].includes(pathname)) return true 
    return false
}
//是否是需要登录查看的页面
export function isNoNeedUserInfo(pathname:string){
    if([HOME_PATHNAME,LOGIN_PATHNAME,REGISTER_PATHNAME].includes(pathname)) return true 
    return false
}